package com.zq.qq2;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by xuebiao on 15/12/17.
 */
public class FileUtils {
    public final static JSONObject GetJson(String ctx) {
        try {
            return new JSONObject(ctx);
        } catch (JSONException e) {

        }
        return new JSONObject();
    }
    public final static String GetString(JSONObject jsonObject, String key) {
        try {
            String val = jsonObject.getString(key);
            if (TextUtils.isEmpty(val))
                return "";
            return val.trim();
        } catch (JSONException e) {
        }
        return "";
    }
    public final static boolean GetBoolean(JSONObject json, String key, boolean def) {
        try {
            return json.getBoolean(key);
        } catch (JSONException e) {

        }
        return def;
    }
    public final static String read(String filePath) {
        try {
            File file = new File(filePath);
            if ( !file.exists() ) {
                return "";
            }
            byte[] temp = new byte[1024];
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int length;
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

            while( (length = bis.read(temp)) > -1 ) {
                baos.write(temp, 0, length);
            }
            String t = new String(baos.toByteArray(), "GBK");
            t =  t.replaceAll("\r", "").replaceAll("\n\n", "\n");
            return t;

        } catch (Exception e) {

        }
        return "";
    }
    public final static void write(String filePath, String content) {
        try {
            File file = new File(filePath);
            if ( !file.exists() ) {
                file.createNewFile();
            }
            if ( file.isDirectory() ) {
                file.delete();
                file.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(content.getBytes());
            bos.flush();
            bos.close();
        } catch (Exception e) {

        }
    }
    public final static void Write(String filePath, String[] content) {
        try {
            File file = new File(filePath);
            if ( !file.exists() ) {
                file.createNewFile();
            }
            if ( file.isDirectory() ) {
                file.delete();
                file.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            for(String t : content)
                bos.write(t.getBytes());
            bos.flush();
            bos.close();
        } catch (Exception e) {

        }
    }
}
